package com.suredy.config.ctrl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.suredy.app.porject.tools.FileTool;
import com.suredy.app.software.model.Software;
import com.suredy.app.software.srv.SoftwareSrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.mvc.model.MessageModel;

/**
 * 常用软件管理
 * 
 * @author VIVID.G
 * @since 2016-4-4
 * @version v0.1
 */
@Controller
@RequestMapping("/software")
public class SoftwareMgrCtrl extends BaseCtrl {

	private final String SOFTWARE_DIR = "_software";

	@Autowired
	private FileTool fileTool;

	@Autowired
	private SoftwareSrv softwareSrv;

	@RequestMapping("/manage")
	public String manage(Software search, Integer page, Integer pageSize, Model model) {
		String view = "app/software/manage";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		List<Software> data = this.softwareSrv.readPageByEntity(search, page, pageSize);

		model.addAttribute("search", search);
		model.addAttribute("data", data);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.softwareSrv.getCountByEntity(search));

		return view;
	}

	@RequestMapping("/index")
	public String index(Software search, Integer page, Integer pageSize, Model model) {
		String view = "app/software/index";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		List<Software> data = this.softwareSrv.readPageByEntity(search, page, pageSize);

		model.addAttribute("search", search);
		model.addAttribute("data", data);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.softwareSrv.getCountByEntity(search));

		return view;
	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public Object newSoftwareForm(String version, @RequestParam(value = "file", required = false) MultipartFile file) {
		if (file == null)
			return MessageModel.createErrorMessage("无效的软件信息", null);

		try {
			File tmp = this.fileTool.store(file.getInputStream(), this.SOFTWARE_DIR);

			String fileName = file.getOriginalFilename();

			Software s = new Software();
			s.setName(fileName);
			s.setVersion(version);
			s.setPath(this.fileTool.relativelyPath(tmp));
			s.setUpTime(new Date());
			s.setDownTimes(0);

			s = this.softwareSrv.save(s);
		} catch (IOException e) {
			return MessageModel.createErrorMessage("上传文件失败！", null);
		}

		return MessageModel.createSuccessMessage("操作成功", null);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	@Transactional
	public Object deleteSoftwareForm(String[] ids) {
		if (ids == null || ids.length < 1)
			return MessageModel.createErrorMessage("删除失败，请选择软件！", null);

		for (String id : ids) {
			Software sw = this.softwareSrv.get(id);

			if (sw != null)
				this.softwareSrv.delete(sw);
		}

		return MessageModel.createSuccessMessage("操作成功", null);
	}

}
